import CryptoJS from 'crypto-js'
 
/**
 * AES加密处理（CBC模式）
 */
export function encryptCBC(word, keyStr, ivStr) {
    keyStr = keyStr ? keyStr : "abcdefgabcdefg12";
    ivStr = ivStr ? ivStr : "!@#$%asdfg123456";
    let key = CryptoJS.enc.Utf8.parse(keyStr);
    let iv = CryptoJS.enc.Utf8.parse(ivStr);
    let srcs = CryptoJS.enc.Utf8.parse(word);
    let encrypted = CryptoJS.AES.encrypt(srcs, key, {
        iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
    })
    return encrypted.toString()
}
/**
 * AES解密处理（CBC模式）
 */
export function decryptCBC(word, keyStr, ivStr) { 
    keyStr = keyStr ? keyStr : "abcdefgabcdefg12";
    ivStr = ivStr ? ivStr : "!@#$%asdfg123456";
    var key = CryptoJS.enc.Utf8.parse(keyStr);
    var iv = CryptoJS.enc.Utf8.parse(ivStr);

    try {
        var decrypt = CryptoJS.AES.decrypt(word, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.ZeroPadding
        });

        try {
            var decryptedString = decrypt.toString(CryptoJS.enc.Utf8);
            return decryptedString;
        } catch (utf8Error) {
            console.error('不是有效的Utf8', utf8Error);
            return '';
        }
    } catch (decryptError) {
        console.error('密钥、IV、密文格式错误', decryptError);
    }
}